<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from '@/views/components/mixins/resize.js'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ month, orders } = {}) {
      this.chart.setOption({
        xAxis: {
          data: month,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#d7d9dd',
              // width: 1, //这里是为了突出显示加上的
            }
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#565759",
              fontSize: 16
            }
          }
        },
        grid: {
          left: 60,
          right: 60,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#565759",
              fontSize: 16
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: "dashed",
              color: "#d7d9dd"
            }
          }
        },
        legend: {
          data: ['订单数']
        },
        series: [
          {
            name: '订单数',
            smooth: false,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  color: echarts.graphic.LinearGradient(0, 1, 0, 0, [
                    {
                      offset: 0.1,
                      color: 'rgb(78, 167, 250,0.1)'
                    },
                    {
                      offset: 0.3,
                      color: 'rgb(78, 167, 250,0.6)'
                    },
                    {
                      offset: 1,
                      color: 'rgb(78, 167, 250,1)'
                    }
                  ])
                }
              }
            },
            lineStyle: {
              width: 2,
              color: "#ff6600",
            },
            areaStyle: {},
            data: orders,
            // animationDuration: 2800,
            // animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>
